<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>
			添加房产信息
		</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<link rel="stylesheet" href="css/main.css" media="all">
		<script type="text/javascript" src="lib/loading/okLoading.js"></script>
		<script src="js/base.js"></script>
		<style>
			.imgs {
				display: none;
			}
		</style>
	</head>
	<body>
		<div class="x-body">
			<form class="layui-form layui-form-pane" action="" id="add">
				<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
					<div class="layui-tab-content">
						<div class="layui-tab-item layui-show">
							<input type="hidden" name="id" value="" />

							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>所属小区
								</label>
								<div class="layui-input-block">
									<select name="community_id" id="community_id" lay-filter="communityChange">

									</select>

								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>栋数
								</label>
								<div class="layui-input-block">
									<select name="building_id" id="building_id">
										<option value="0">请选择</option>
									</select>
									<script>
										let fangchanString = localStorage.getItem("homeedit");
										let fangchan = JSON.parse(fangchanString);

										let url = "http://localhost:8080/wuye/XiaoQuController/selectXiaoQuAll";
										rest(url, "get", null, function(r) {
											let arr = r.data;
											let innerHTML = "";
											for (let i = 0; i < arr.length; i++) {
												if (fangchan.suoshuxiaoqu == arr[i].mingcheng) {
													innerHTML = innerHTML + " <option value=\"" + arr[i].mingcheng + "\" selected>" + arr[i]
														.mingcheng +
														"</option>";
												} else {
													innerHTML = innerHTML + " <option value=\"" + arr[i].mingcheng + "\" >" + arr[i]
														.mingcheng +
														"</option>";
												}
											}
											document.getElementById("community_id").innerHTML = innerHTML;
											//数据设置给了第一个select
											//拿第一个select里边第一行数据里的参数查询数据库，拿到第二个select里应该设置options
										});
									</script>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>房产名称
								</label>
								<div class="layui-input-block">
									<input type="text" name="home_name" id="fangchanmingcheng" autocomplete="off"
										placeholder="空制在80个汉字，160个字符以内" class="layui-input" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>户主名称
								</label>
								<div class="layui-input-block">
									<input type="text" name="householder" id="huzhumingcheng" autocomplete="off"
										placeholder="" class="layui-input" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>联系方式
								</label>
								<div class="layui-input-block">
									<input type="text" name="phone" id="lianxifangshi" autocomplete="off" placeholder=""
										class="layui-input" lay-verify="required|phone" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>房间数
								</label>
								<div class="layui-input-block">
									<input type="text" name="home_number" id="fangjianshu" autocomplete="off"
										placeholder="" class="layui-input" lay-verify="required|number" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>单元
								</label>
								<div class="layui-input-block">
									<input type="text" name="unit_building" id="danyuan" autocomplete="off"
										placeholder="请输入数字" class="layui-input" lay-verify="required|number" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>楼层
								</label>
								<div class="layui-input-block">
									<input type="text" name="floor" id="louceng" autocomplete="off" placeholder="请输入数字"
										class="layui-input" lay-verify="required|number" value="">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">
									<span class='x-red'>*</span>入住时间
								</label>

								<div class="layui-input-inline">
									<input class="layui-input" name="rz_time" id="ruzhushijian" placeholder="入住时间"
										value="">
								</div>

							</div>
							<div class="layui-form-item layui-form-text">
								<label class="layui-form-label">
									<span class='x-red'>*</span>描述
								</label>
								<div class="layui-input-block">
									<textarea placeholder="请输入内容" id="miaoshu" name="desc"
										class="layui-textarea"></textarea>
								</div>
							</div>

							<div class="layui-form-item">
								<button class="layui-btn" type="button" lay-filter="add" lay-submit="">
									保存
								</button>
							</div>
							<!--</form>-->
							<div style="height:100px;"></div>
						</div>

					</div>

				</div>
			</form>
		</div>
		<script src="lib/layui/layui.js" charset="utf-8"></script>
		<script src="js/x-layui.js" charset="utf-8"></script>
		<script src="js/jquery.min.js" charset="utf-8"></script>
		<!--<script src="/static/xiyuan/js/jquery.js" charset="utf-8"></script>-->
		<script>
			document.getElementById('ruzhushijian').onclick = function() {
				start.elem = this;
				laydate(start);
			};
			document.getElementById("community_id").value = fangchan.suoshuxiaoqu;
			// document.getElementById("building_id").value = fangchan.dongshu;
			document.getElementById("fangchanmingcheng").value = fangchan.fangchanmingcheng;
			document.getElementById("huzhumingcheng").value = fangchan.huzhumingcheng;
			document.getElementById("lianxifangshi").value = fangchan.lianxifangshi;
			document.getElementById("fangjianshu").value = fangchan.fangjianshu;
			document.getElementById("danyuan").value = fangchan.danyuan;
			document.getElementById("louceng").value = fangchan.louceng;
			document.getElementById("miaoshu").value = fangchan.miaoshu;
			document.getElementById("ruzhushijian").value = fangchan.ruzhushijian;

			layui.use(['laydate', 'element', 'layer', 'form'], function() {
				$ = layui.jquery; //jquery

				laydate = layui.laydate; //日期插件
				lement = layui.element(); //面包导航
				layer = layui.layer; //弹出层

				form = layui.form();
				okLoading.close($);
				form.render();
				var start = {
					min: laydate.now(),
					max: '2099-06-16 23:59:59',
					istoday: false,
					choose: function(datas) {
						//end.min = datas; //开始日选好后，重置结束日的最小日期
						//end.start = datas //将结束日的初始值设定为开始日
					}
				};



				//监听提交
				form.on('submit(add)', function(data) {
					let suoshuxiaoqu = document.getElementById("community_id").value;
					let dongshumingcheng = document.getElementById("building_id").value;
					let fangchanmingcheng = document.getElementById("fangchanmingcheng").value;
					let huzhumingcheng = document.getElementById("huzhumingcheng").value;
					let lianxifangshi = document.getElementById("lianxifangshi").value;
					let fangjianshu = document.getElementById("fangjianshu").value;
					let danyuan = document.getElementById("danyuan").value;
					let louceng = document.getElementById("louceng").value;
					let miaoshu = document.getElementById("miaoshu").value;

					let url = "http://localhost:8080/wuye/FangChanController/edit";
					let method = "post";
					let json = {};
					json.id = fangchan.id;
					json.suoshuxiaoqu = suoshuxiaoqu;
					json.dongshumingcheng = dongshumingcheng;
					json.fangchanmingcheng = fangchanmingcheng;
					json.huzhumingcheng = huzhumingcheng;
					json.lianxifangshi = lianxifangshi;
					json.fangjianshu = fangjianshu;
					json.danyuan = danyuan;
					json.louceng = louceng;
					json.miaoshu = miaoshu;
					rest(url, method, json, function(r) {
						if (r.code == 200) {
							//添加成功
							layer.msg("修改成功", {
								icon: 6,
								time: 2000
							}, function() {
								window.parent.location.reload();
								var index = parent.layer.getFrameIndex(window.name);
								parent.layer.close(index);
							});
						} else {
							layer.msg("修改失败", {
								icon: 5,
								time: 2000
							});
							return false;
						}
					});
					return false;
				});


				let communityChange = function(data) {
					let XiaoQuchange = document.getElementById("community_id");
					var communityName = XiaoQuchange.value; // 获取选中的小区名字  
					communidy(communityName); // 根据小区名字获取栋数数据    
				};
				form.on('select(communityChange)', communityChange);

				function communidy(communityName) {
					// 根据小区名字获取栋数数据
					let url = "http://localhost:8080/wuye/DongShuController/findDongShuByXiaoQuName";
					let json = {};
					json.communityName = communityName;
					rest(url, "post", json, function(r) {

						let arr = r.data;
						let innerHTML = "";
						for (let i = 0; i < arr.length; i++) {
							let dongshu = fangchan.dongshu;
							if (dongshu == arr[i].dongshumingcheng) {
								innerHTML = innerHTML + " <option value=\"" + arr[i].dongshumingcheng +
									"\" selected>" +
									arr[i].dongshumingcheng + "</option>";
							} else {
								innerHTML = innerHTML + " <option value=\"" + arr[i].dongshumingcheng + "\" >" +
									arr[i].dongshumingcheng + "</option>";
							}
						}
						document.getElementById("building_id").innerHTML = innerHTML;


						layui.form().render('select', {
							elem: '#building_id'
						});
					});
				}

				communityChange(null);


			})
		</script>


	</body>
</html>